package com.ly.mpayment.persist.service.yidong;

import java.io.Serializable;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.ly.mpayment.persist.entity.yidong.GameOrder;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;

import cn.jpush.api.utils.StringUtils;

@Service
public class GameOrderServiceImpl extends AbstractService<GameOrder, Serializable> implements IGameOrderService {

	@Override
	public GameOrder saveGameOrder(GameOrder order) {
		if(order != null){
			order = this.saveOrUpdate(order);
			return order;
		}
		return null;
	}

	@Override
	public boolean updateGameOrder(GameOrder order) {
		if(order != null){
			this.updateEntity(order);
			return true;
		}
		return false;
	}

	@Override
	public GameOrder findById(String gameId) {
		return this.findUnique("select g from GameOrder g where g.gameId = ? ", gameId);
	}

	@Override
	public GameOrder findByOrderId(String orderId) {
		return this.findUnique("select g from GameOrder g where g.orderId = ? ", orderId);
	}

	@Override
	public GameOrder findByMobileOrderId(String mobileOrderId) {
		return this.findUnique("select g from GameOrder g where g.mobileOrdersId = ? ", mobileOrderId);
	}

	@Override
	public Page<GameOrder> pageOrders(Map<String, Object> parameters, int start, int pageSize) {
		
		String hql = " select g from GameOrder g where 1 = 1  "
				+ " #[ and g.orderId = :orderId ]#  #[ and g.orderAmount = :orderAmount ]# "
				+ " #[ and g.remark = :remark ]#  #[ and g.orderState = :orderState ]# ";
		if(parameters.containsKey(":startTime")){
			hql += " and g.orderTime >= '".concat(parameters.get(":startTime").toString()).concat("'");
			parameters.remove(":startTime");
		}
		if(parameters.containsKey(":endTime")){
			hql += " and g.orderTime <= '".concat(parameters.get(":endTime").toString()).concat("'");
			parameters.remove(":endTime");
		}
		hql += " order by g.orderTime desc";
		return this.findPageByNamed(hql, parameters, start, pageSize);
	}

}
